package com.base.service.impl;

import java.util.List;

import com.base.service.CardTypeService;
import com.common.bean.Cardmaintype;
import com.common.bean.Cardsubtype;
import com.common.util.AbstractManager;
import com.common.util.PagerModel;

public class CardTypeServiceImpl extends AbstractManager implements
		CardTypeService {

	public void addCardmaintype(Cardmaintype cardmaintype) {
		String hql = "select nvl(max(cardmaintypeno),0) from Cardmaintype";
		Long cardmaintypeno = 0l;
		cardmaintypeno = (Long) this.getHibernateTemplate().find(hql).get(0);
		cardmaintypeno += 1;
		
		cardmaintype.setCardmaintypeno(cardmaintypeno);
		this.getHibernateTemplate().save(cardmaintype);
	}

	public void addCardsubtype(Cardsubtype cardsubtype) {
		String hql = "select nvl(max(cardsubtypeno),0) from Cardsubtype";
		Long cardsubtypeno = 0l;
		cardsubtypeno = (Long) this.getHibernateTemplate().find(hql).get(0);
		cardsubtypeno += 1;
		
		cardsubtype.setCardsubtypeno(cardsubtypeno);
		this.getHibernateTemplate().save(cardsubtype);
	}

	@SuppressWarnings("unchecked")
	public List<Cardmaintype> findAllCardmaintype() {
		String hql = "from Cardmaintype";
		return this.getHibernateTemplate().find(hql);
	}

	@SuppressWarnings("unchecked")
	public List<Cardsubtype> findAllCardsubtype() {
		String hql = "from Cardsubtype";
		return this.getHibernateTemplate().find(hql);
	}

	public Cardmaintype findCardmaintypeById(long cardmaintypeno) {
		return (Cardmaintype) this.getHibernateTemplate().load(Cardmaintype.class, cardmaintypeno);
	}

	public Cardmaintype findCardmaintypeByName(String cardmaintypename) {
		String hql = "from Cardmaintype mt where mt.cardmaintypename = ?";
		return (Cardmaintype) this.getHibernateTemplate().find(hql, cardmaintypename).get(0);
	}

	public PagerModel findCardmaintypeForPager() {
		String hql = "from Cardmaintype";
		return searchPaginated(hql);
	}

	public Cardsubtype findCardsubtypeById(long cardsubtypeno) {
		return (Cardsubtype) this.getHibernateTemplate().load(Cardsubtype.class, cardsubtypeno);
	}

	public Cardsubtype findCardsubtypeByName(String cardsubtypename) {
		String hql = "from Cardsubtype mt where mt.cardsubtypename = ?";
		return (Cardsubtype) this.getHibernateTemplate().find(hql, cardsubtypename).get(0);
	}

	public PagerModel findCardsubtypeForPager(long cardmaintypeno) {
		String hql = "from Cardsubtype sub where sub.cardmaintype.cardmaintypeno = ?";
		return searchPaginated(hql,cardmaintypeno);
	}

	public void removeCardmaintype(long cardmaintypeno) {
		String hql = "from Cardsubtype sub where sub.cardmaintype.cardmaintypeno = ?";
		List list = this.getHibernateTemplate().find(hql,cardmaintypeno);
		if(list != null && list.size() > 0) {
			throw new RuntimeException("this main type is used, can not delete.");
		}
		
		Cardmaintype cardmaintype = (Cardmaintype) this.getHibernateTemplate().load(Cardmaintype.class, cardmaintypeno);
		this.getHibernateTemplate().delete(cardmaintype);
	}

	public void removeCardsubtype(long cardsubtypeno) {
		Cardsubtype cardsubtype = (Cardsubtype) this.getHibernateTemplate().load(Cardsubtype.class, cardsubtypeno);
		this.getHibernateTemplate().delete(cardsubtype);
	}

	public void updateCardmaintype(Cardmaintype cardmaintype) {
		this.getHibernateTemplate().saveOrUpdate(cardmaintype);
	}

	public void updateCardsubtype(Cardsubtype cardsubtype) {
		this.getHibernateTemplate().saveOrUpdate(cardsubtype);
	}
}
